package com.ihg.hiex.admin.handler.support;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.velocity.app.VelocityEngine;
import org.springframework.ui.velocity.VelocityEngineUtils;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.BaseFont;
/**
 * 
 * @author houtao
 *
 */
@SuppressWarnings("deprecation")
public class PDFTemplate {
    
    private static final Log logger = LogFactory.getLog(PDFTemplate.class);
    
    private String fileName;
    
    private String text;
    
    private boolean needFont = true;
    
    private String fontPath;
    
    /**
     * @param fileName
     */
    public PDFTemplate(String fileName) {
        this.fileName = fileName;
    }
    
    /**
     * @param velocityEngine
     * @param model
     * @param contentPath
     * @return
     */
    public PDFTemplate initialize(VelocityEngine velocityEngine, Map<String, Object> model, String contentPath) {
        
    	this.text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, contentPath, "utf-8", model);
    	
    	logger.info(StringUtils.contains(contentPath, "en.vm"));
    	
        if(StringUtils.contains(contentPath, "en.vm")) {
        	this.needFont = false;
        } else {
        	this.needFont = true;
        }
        
        return this;
    }
    
    /**
     * 
     * @param velocityEngine
     * @param model
     * @param contentPath
     * @param fontPath
     * @return
     */
    public PDFTemplate initialize(VelocityEngine velocityEngine, Map<String, Object> model, String contentPath, String fontPath) {
        
        this.text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, contentPath, "utf-8", model);
        
        logger.info(StringUtils.contains(contentPath, "en.vm"));
        
        if(StringUtils.contains(contentPath, "en.vm")) {
            this.needFont = false;
        } else {
            this.needFont = true;
        }
        
        this.fontPath = fontPath;
        
        return this;
    }
    
    /**
     * @return the fileName
     */
    public String getFileName() {
        return fileName;
    }

    /**
     * @param fileName the fileName to set
     */
    public void setFileName(String fileName) {
        this.fileName = fileName;
    }
    
    /**
     * 
     * @param uploadPath
     * @param filePath
     * @return
     * @throws IOException
     * @throws DocumentException
     */
    public String toPDF(String uploadPath, String filePath, String cloudHost) throws IOException, DocumentException {
        
        logger.info("Start exporting file: " + this.fileName);
        
        File file = new File(uploadPath + "/" + filePath + "/" + this.fileName);
        
        OutputStream os = new FileOutputStream(file);
        
        ITextRenderer renderer = new ITextRenderer();
        
        // 解决中文支持问题  
        if(needFont) {
          ITextFontResolver fontResolver = renderer.getFontResolver();
          logger.info("add font");
          String classPath = this.getClass().getClassLoader().getResource("").getPath(); 
          String sourcePath = StringUtils.splitByWholeSeparator(classPath, "/wtw-ihg-admin/")[0];
          String path = sourcePath + "/" + fontPath;
          logger.info("toPDF fontPath is : " + path);
          fontResolver.addFont(path, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); 
        }
        
        
        
        text = text.replace("&", "&amp;").replace("&amp;nbsp", "&nbsp");
        logger.info("start set document string when exporting file: ");
        renderer.setDocumentFromString(text);
        
        renderer.layout();
        
        renderer.createPDF(os);
          
        os.close();
        
        logger.info("End exporting file: ");
        
        return cloudHost + "/" + filePath + "/" + this.fileName;
        
    }
}
